import React from 'react';

const useSessionStorage = (name: string, value: any) => {
    const [sessionValue, setSessionValue] = React.useState(() => {
        const storedValue = sessionStorage.getItem(name);
        return storedValue ? JSON.parse(storedValue) : value;
    });

    React.useEffect(() => {
        sessionStorage.setItem(name, JSON.stringify(sessionValue));
    }, [name, sessionValue]);

    return [sessionValue, setSessionValue] as const;
}

export default useSessionStorage;